//
//  SureGongXuDetailViewController.m
//  MyProject
//
//  Created by Pei on 16/7/31.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "SureGongXuDetailViewController.h"
#import "SSCheckBoxView.h"
#import "YMCaptureViewController.h"
#import "UserInfo.h"
#import "UIImageView+WebCache.h"
#import "JZAlbumViewController.h"

@interface SureGongXuDetailViewController ()<UITableViewDelegate, UITableViewDataSource,UIActionSheetDelegate, UITextViewDelegate>
{
    UIScrollView *_mainScrollView;
    
    UIView *_headerView;
    
    UITableView *_myTableView;
    
    SSCheckBoxView *_boxView;
    SSCheckBoxView *_boxView1;
    SSCheckBoxView *_boxView2;
    
    SSCheckBoxView *_currentBox;
    
    UIView *_checkView;
    
    UIView *_inputView;
    
    NSDictionary *currentDic;
    
    int status;
    
    UITextView *inputField;
    
    NSMutableArray *_imageArray;
    NSMutableArray *_imageDataArray;
    UIView *lineView04;
    UIButton *addBtn;
    
    UIView *pishiView;
    UITextView *pishiField;
    
    UITextView *_currentField;
    
    NSArray *imageUrlList1;
    
    
}

@end

@implementation SureGongXuDetailViewController

- (id)initWithData:(NSDictionary*)dic{
    self = [super init];
    if (self) {
        currentDic = dic;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    

    
    _imageArray = [[NSMutableArray alloc] initWithCapacity:3];
    _imageDataArray = [[NSMutableArray alloc] initWithCapacity:3];
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 22)];
    [rightBtn setTitle:@"确认" forState:UIControlStateNormal];
    [rightBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [rightBtn addTarget:self action:@selector(rightBtnAcion) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightBtn]];
    
    
    
    _mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
    _mainScrollView.contentSize = CGSizeMake(kDeviceWidth, _mainScrollView.height+1.0f);
    _mainScrollView.backgroundColor = kWhiteColor;
    [self.view addSubview:_mainScrollView];
    
    [self initHeaderView];
    [self initTableView];
    [self initCheckBox];
    [self initBottomView];
}



#pragma mark **************************** UITextField Delegate ************************
- (void)textViewDidBeginEditing:(UITextView *)textView{
    _currentField = textView;
}


- (void)initHeaderView{
    
    _headerView = [[UIView alloc] init];
    _headerView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, 40.0f+0.5f);
    [_mainScrollView addSubview:_headerView];
    
    UIView *lineView0 = [[UIView alloc] init];
    lineView0.frame = CGRectMake(0.0f, 40.0f, kDeviceWidth, 0.5f);
    lineView0.backgroundColor = kGreenColor;
    [_headerView addSubview:lineView0];
    
    
    UIControl *lbl0Control = [[UIControl alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kDeviceWidth/2.0f, 40.0f)];
    lbl0Control.tag = 116;
    [lbl0Control addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:lbl0Control];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(kDeviceWidth/2.0f, 0.0f, 0.5f, 40.0f);
    lineView.backgroundColor = kGreenColor;
    [_headerView addSubview:lineView];
    
    UIControl *lbl1Control = [[UIControl alloc] initWithFrame:CGRectMake(kDeviceWidth/2.0f, 0.0f, kDeviceWidth/2.0f, 40.0f)];
    lbl1Control.tag = 118;
    //[lbl1Control addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:lbl1Control];
    
    //ic_down@2x.png
    UILabel *lbl0 = [[UILabel alloc] init];
    lbl0.frame = CGRectMake(0.0, (40-(Home_Small_Size+2.0f))/2.0f, kDeviceWidth/2.0f-26/2-2.0f, Home_Small_Size+2.0f);
    lbl0.font = kAppFont(Home_Small_Size+2.0f);
    lbl0.textColor = kBlackColor;
    lbl0.text = [currentDic valueForKey:@"UnitName"];
    lbl0.textAlignment = NSTextAlignmentLeft;
    [lbl0Control addSubview:lbl0];
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_down"]];
    arrowImageView.frame = CGRectMake(kDeviceWidth/2.0f-26.0f/2.0f-2.0f, (40-11.0f/2.0f)/2.0f, 26/2, 11.0f/2.0f);
    [lbl0Control addSubview:arrowImageView];
    
    UILabel *lbl1 = [[UILabel alloc] init];
    lbl1.frame = CGRectMake(0.0f, (40-(Home_Small_Size+2.0f))/2.0f, kDeviceWidth/2.0f-26/2-2.0f, Home_Small_Size+2.0f);
    lbl1.font = kAppFont(Home_Small_Size+2.0f);
    lbl1.textColor = kBlackColor;
    lbl1.text = [currentDic valueForKey:@"ProjectName"];
    lbl1.textAlignment = NSTextAlignmentLeft;
    [lbl1Control addSubview:lbl1];
    
    UIImageView *arrowImageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_down"]];
    arrowImageView1.frame = CGRectMake(kDeviceWidth/2.0f-26.0f/2.0f-2.0f, (40-11.0f/2.0f)/2.0f, 26/2, 11.0f/2.0f);
    [lbl1Control addSubview:arrowImageView1];
}

- (void)initTableView{
    
    UIView *tableHeadView = [[UIView alloc] init];
    tableHeadView.frame = CGRectMake(0.0f, _headerView.bottom, kDeviceWidth, 40.0f);
    [_mainScrollView addSubview:tableHeadView];
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.frame = CGRectMake(10.0f, (40-Home_Big_Size)/2.0f, kDeviceWidth-10*2, Home_Big_Size);
    titleLbl.font = kAppFont(Home_Big_Size);
    titleLbl.text = @"所属关键工序";
    [tableHeadView addSubview:titleLbl];
    
    //UIView *lineView = [[UIView alloc] init];
    //lineView.frame = CGRectMake(0.0f, 39.5f, kDeviceWidth, 0.5f);
    //lineView.backgroundColor = kGreenColor;
    //[tableHeadView addSubview:lineView];
    
    _myTableView = [[UITableView alloc] init];
    _myTableView.frame = CGRectMake(20.0f, tableHeadView.bottom, kDeviceWidth-20.0f, 30*4);
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.scrollEnabled = NO;
    [_mainScrollView addSubview:_myTableView];
    
}

- (void)initCheckBox{
    _checkView = [[UIView alloc] init];
    _checkView.frame = CGRectMake(0.0f, _myTableView.bottom, kDeviceWidth, 40.0f);
    [_mainScrollView addSubview:_checkView];
    
    UIView *lineView0 = [[UIView alloc] init];
    lineView0.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, 0.5f);
    lineView0.backgroundColor = kGreenColor;
    //[_checkView addSubview:lineView0];
    
    UIView *lineView1 = [[UIView alloc] init];
    lineView1.frame = CGRectMake(0.0f, 39.5f, kDeviceWidth, 0.5f);
    lineView1.backgroundColor = kGreenColor;
    [_checkView addSubview:lineView1];
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.frame = CGRectMake(10.0f, (40-Home_Big_Size)/2.0f, 40.0f, Home_Big_Size);
    titleLbl.font = kAppFont(Home_Big_Size);
    titleLbl.text = @"状态";
    [_checkView addSubview:titleLbl];
    
    _boxView = [[SSCheckBoxView alloc] initWithFrame:CGRectMake(titleLbl.right, 10.0f, 80, 20.0f) style:kSSCheckBoxViewStyleMyStyle checked:NO];
    [_boxView setText:@"未开始"];
    
    _boxView.tag = 116;
    [_boxView setStateChangedTarget:self selector:@selector(boxViewAction:)];
    [_checkView addSubview:_boxView];
    
    _boxView1 = [[SSCheckBoxView alloc] initWithFrame:CGRectMake(_boxView.right+10.0f, 10.0f, 80, 20.0f) style:kSSCheckBoxViewStyleMyStyle checked:NO];
    [_boxView1 setText:@"施工中"];
    _boxView1.tag = 118;
    [_boxView1 setStateChangedTarget:self selector:@selector(boxViewAction:)];
    [_checkView addSubview:_boxView1];
    
    _boxView2 = [[SSCheckBoxView alloc] initWithFrame:CGRectMake(_boxView1.right+10.0f, 10.0f, 80, 20.0f) style:kSSCheckBoxViewStyleMyStyle checked:NO];
    [_boxView2 setText:@"已竣工"];
    _boxView2.tag = 119;
    [_boxView2 setStateChangedTarget:self selector:@selector(boxViewAction:)];
    [_checkView addSubview:_boxView2];
    
    if ([[currentDic valueForKey:@"Status"] intValue] == 0) {
        [_boxView setChecked:YES];
        _currentBox = _boxView;
    }else if ([[currentDic valueForKey:@"Status"] intValue] == 1){
        [_boxView1 setChecked:YES];
        _currentBox = _boxView1;
    }else{
        [_boxView2 setChecked:YES];
        _currentBox = _boxView2;
    }
}

- (void) initBottomView{
    
    _inputView = [[UIView alloc] init];
    _inputView.frame = CGRectMake(0.0f, _checkView.bottom, kDeviceWidth, 100.0f);
    [_mainScrollView addSubview:_inputView];
    
    UILabel *inputLbl = [[UILabel alloc] init];
    inputLbl.frame = CGRectMake(10.0, 20.0f, 40.0f, Home_Small_Size+2.0f);
    inputLbl.font = kAppFont(Home_Small_Size+2.0f);
    inputLbl.textColor = kBlackColor;
    inputLbl.text = @"描述";
    [_inputView addSubview:inputLbl];
    
    inputField = [[UITextView alloc] init];
    inputField.frame = CGRectMake(inputLbl.right, inputLbl.origin.y, kDeviceWidth-10*3-40.0f, 100-20-10);
    inputField.font = kAppFont(Home_Small_Size+2.0f);
    inputField.textColor = kGreenColor;
    inputField.delegate =self;
    inputField.text = [currentDic valueForKey:@"SPRemark"];
    [_inputView addSubview:inputField];
    
    lineView04 = [[UIView alloc] init];
    lineView04.frame = CGRectMake(0.0f, _inputView.height-0.5f, kDeviceWidth, 0.5f);
    lineView04.backgroundColor = kGreenColor;
    [_inputView addSubview:lineView04];
    
    int totalloc = 4;
    CGFloat tempWidth = (kDeviceWidth-10.0f*5.0f)/4.0f;
    CGFloat tempHeight = (kDeviceWidth-10.0f*5.0f)/4.0f;
    
    
    NSArray *imageA1 = [currentDic objectForKey:@"AttachList"];
    imageUrlList1 = imageA1;
    
    int margin = 10;
    float yyy = 0.0f;
    
    for (int i = 0; i < imageA1.count; i ++) {
        
        int row=i/totalloc;//行号
        //1/3=0,2/3=0,3/3=1;
        int loc=i%totalloc;//列号
        
        CGFloat appviewx = margin+(margin+tempWidth)*loc;
        CGFloat appviewy = margin+(margin+tempHeight)*row;
        
        
        
        UIImageView *tempImageView = [[UIImageView alloc] init];
        NSString *imageUrl = [imageA1 objectAtIndex:i];
        
        tempImageView.userInteractionEnabled=YES;
        tempImageView.tag = i;
        UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickImage:)];
        [tempImageView addGestureRecognizer:singleTap];
        
        tempImageView.frame = CGRectMake(appviewx, _inputView.bottom+appviewy, tempWidth, tempHeight);
        [tempImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"normalDefault"]];
        tempImageView.contentMode = UIViewContentModeScaleToFill;
        //[tempImageView setClipsToBounds:YES];
        
        tempImageView.backgroundColor = [UIColor grayColor];
        [_mainScrollView addSubview:tempImageView];
        
        yyy = tempImageView.bottom;
    }
    
    if (imageA1 == nil || imageA1.count <= 0) {
        yyy = _inputView.bottom;
    }
    
    pishiView = [[UIView alloc] init];
    pishiView.frame = CGRectMake(0.0f, yyy, kDeviceWidth, 100.0f);
    //pishiView.frame = CGRectMake(0.0f, _inputView.bottom, kDeviceWidth, 100.0f);
    [_mainScrollView addSubview:pishiView];
    
    UIView *lineViewBB = [[UIView alloc] init];
    lineViewBB.frame = CGRectMake(0.0f, 6.0f, kDeviceWidth, 0.5f);
    lineViewBB.backgroundColor = kGreenColor;
    [pishiView addSubview:lineViewBB];

    
    
    UILabel *pishiLbl = [[UILabel alloc] init];
    pishiLbl.frame = CGRectMake(10.0, 20.0f, 40.0f, Home_Small_Size+2.0f);
    pishiLbl.font = kAppFont(Home_Small_Size+2.0f);
    pishiLbl.textColor = kBlackColor;
    pishiLbl.text = @"批示";
    [pishiView addSubview:pishiLbl];
    
    pishiField = [[UITextView alloc] init];
    pishiField.frame = CGRectMake(inputLbl.right, inputLbl.origin.y, kDeviceWidth-10*3-40.0f, 100-20-10);
    pishiField.font = kAppFont(Home_Small_Size+2.0f);
    pishiField.textColor = kGreenColor;
    pishiField.delegate = self;
    [pishiView addSubview:pishiField];
    
    lineView04 = [[UIView alloc] init];
    lineView04.frame = CGRectMake(0.0f, pishiView.height-0.5f, kDeviceWidth, 0.5f);
    lineView04.backgroundColor = kGreenColor;
    [pishiView addSubview:lineView04];
    
    _mainScrollView.contentSize = CGSizeMake(kDeviceWidth, pishiView.bottom+100);
    
}



- (void)onClickImage:(UITapGestureRecognizer*)sender{
    JZAlbumViewController *jzAlbumVC = [[JZAlbumViewController alloc] init];
    
    jzAlbumVC.currentIndex = sender.view.tag;//这个参数表示当前图片的index，默认是0
    
    jzAlbumVC.imgArr = [[NSMutableArray alloc] initWithArray:imageUrlList1];//图片数组，可以是url，也可以是UIImage
    [self.navigationController pushViewController:jzAlbumVC animated:YES];
}


- (void)addBtnAction{
    [inputField resignFirstResponder];
    
    UIActionSheet *chooseImageSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
    [chooseImageSheet showInView:self.view];
}

- (void)boxViewAction:(SSCheckBoxView*)sender
{
    if (sender == _currentBox)
    {
        return;
    }
    
//    _currentBox.checked = NO;
//    sender.checked = YES;
//    _currentBox = sender;
//    
//    if (sender.tag == 116) {
//        status = 0;
//    }else if (sender.tag == 118){
//        status = 1;
//    }else{
//        status = 2;
//    }
}


- (void)controlAction:(UIControl*)sender{
    
}

- (void)rightBtnAcion{
    NSDictionary *body = [NSDictionary dictionaryWithObjectsAndKeys:
                          [[UserInfo getInstance] getUserID],@"UserID",
                          [NSNumber numberWithInt:[[currentDic valueForKey:@"ID"] intValue]],@"ProcessId",
                          pishiField.text,@"Remark",
                          nil];
    [[YMNetLib getInstance] connectWithAction:Sure_GuanJianGongXu method:@"POST" withContent:@"" withBody:body withDelegate:self];
}

- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
        if ([self.delegate respondsToSelector:@selector(passRloadData)]) {
            [self.delegate passRloadData];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"first_cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.textColor = kRGB(205, 206, 207);
    cell.textLabel.font = kAppFont(Home_Small_Size+2);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 0) {
        cell.textLabel.text = [NSString stringWithFormat:@"1.%@", [currentDic valueForKey:@"Name"]];
    }else if (indexPath.row ==1){
        cell.textLabel.text = [NSString stringWithFormat:@"2.%@", [currentDic valueForKey:@"SpecialProject"]];
    }else if (indexPath.row ==2){
        cell.textLabel.text = [NSString stringWithFormat:@"3.%@", [currentDic valueForKey:@"PartProject"]];
    }else{
        cell.textLabel.text = [NSString stringWithFormat:@"4.%@", [currentDic valueForKey:@"UnitProject"]];
    }

    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30.0f;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}





@end
